<?php
/**
  Document   : presents
  Created on : 19.06.2013., 00.40.53
  Author     : Sasa
  Description:
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Presents extends MY_Controller {

    public function __construct() {
      parent::__construct();
      // Ako korisnik nije ulogovan nemoze na stranice
      if( false == $this->session->userdata('user_logged') )
         redirect(base_url(), 'refresh');
      
      $this->load->model('presents_mod');
      $this->load->model('users_mod');
      $this->load->model('images_mod');      
    }
    
    public function index()
    {
        $data['title'] = lang('presents_title');
        $data['keywords'] = lang('presents_keywords');
        $data['description'] = lang('presents_description');
        try{
          $id = $this->session->userdata('user_id');
          $data['user'] = $this->users_mod->getUserData($id,false);
          $data['main_image'] = $this->images_mod->getMainImage($id);
          $data['presents'] = $this->presents_mod->getMyPresentsList($id);
        } 
        catch( Exception $e )
        {
          $data['user'] = NULL;
          $data['main_image'] = NULL;
          $data['presents'] = NULL;
        }
        
        $data['menu_active'] = "";//"profile";        
        $data['contents']  = "presents/home";   
		$this->load->view('template/main',$data);
    }
    
    public function present_list()
    {      
      $data['name'] = $this->input->get('name');
      $data['to_id'] = $this->input->get('to_id');
      $this->load->view('presents/present_send_list',$data);
    }
    
    public function sendPresent()
    {
      try{
          
          $present = $this->input->post('present');          
          $to_id = $this->input->post('to_id');
          $ret = $this->presents_mod->sendPresent($present,$to_id);
          if( true == $ret )
          {
            $response = array(
                'ind' => 1,
                'msg' => lang('present_send')
            );
          }
          else if( -2 == $ret)
          {
            $response = array(
                'ind' => -2,
                'msg' => sprintf(lang('user_no_coins'),base_url()."payment.htm")
            );
          }
          else
          {
            $response = array(
                'ind' => 0,
                'msg' => lang('present_not_send')
            );
          }
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    } 
         
    public function setPresentIsView()
    {
      try{
          
          $present_id = $this->input->post('present_id');          
          
          if( true == $this->presents_mod->setPresentIsView($present_id) )
          {
            $id = $this->session->userdata('user_id'); 
            $data['presents'] = $this->presents_mod->getMyPresentsList($id);
            $html = $this->load->view('presents/presents_list',$data,true);
            $response = array(
                'ind' => 1,
                //'msg' => lang('present_send')
                'html' => $html
            );
          }
          
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    
    public function removePresent()
    {
      try{
          
          $present_id = $this->input->post('present_id');          
          
          if( true == $this->presents_mod->removePresent($present_id) )
          {
            //$id = $this->session->userdata('user_id'); 
            //$data['presents'] = $this->presents_mod->getMyPresentsList($id);
            //$html = $this->load->view('presents/presents_list',$data,true);
            $response = array(
                'ind' => 1,
                'msg' => ""
                //'html' => $html
            );
          }
          
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }

} // end class
/* End of file presents.php */
/* Location: ./application/controllers/presents.php */